//
//  RootViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "RootViewController.h"
#import "CommunityTableViewController.h"
#import "FindViewController.h"
#import "CallViewController.h"
#import "FriendViewController.h"
#import "MyCenterViewController.h"
#import "SystemMessage.h"
@interface RootViewController ()<EMChatManagerDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CommunityTableViewController *comVC = [[CommunityTableViewController alloc]init];
    comVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"社区" image:[UIImage imageNamed:@"community"] tag:101];
    UINavigationController *comNC = [[UINavigationController alloc]initWithRootViewController:comVC];
    FindViewController *findVC = [[FindViewController alloc]init];
    findVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"find"] tag:102];
    UINavigationController *findNC = [[UINavigationController alloc]initWithRootViewController:findVC];
    CallViewController *callVC = [[CallViewController alloc]init];
    callVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"语音" image:[UIImage imageNamed:@"call"] tag:103];
    UINavigationController *callNC = [[UINavigationController alloc]initWithRootViewController:callVC];
    FriendViewController *friendVC = [[FriendViewController alloc]init];
    friendVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"知音" image:[UIImage imageNamed:@"friend"] tag:104];
    UINavigationController *friendNC = [[UINavigationController alloc]initWithRootViewController:friendVC];
    MyCenterViewController *myCenterVC = [[MyCenterViewController alloc]init];
    myCenterVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"中心" image:[UIImage imageNamed:@"center"] tag:105];
    UINavigationController *myCenterNC = [[UINavigationController alloc]initWithRootViewController:myCenterVC];
    
    self.viewControllers = @[comNC,findNC,callNC,friendNC,myCenterNC];
    
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    [GLobalRealReachability startNotifier];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kRealReachabilityChangedNotification object:nil];
    
}


- (void)networkChanged:(NSNotification *)notification
{
    //    RealReachability *reachability = (RealReachability *)notification.object;
    //    ReachabilityStatus status = [reachability currentReachabilityStatus];
    //    NSLog(@"currentStatus:%@",@(status));
    
    [GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {
        
        NSString *statuStr = nil;
        switch (status)
        {
            case RealStatusNotReachable:
            {
                
                NSLog(@"没有网络连接");
                
                statuStr = @"没有网络连接";
                
                break;
            }
                
            case RealStatusViaWWAN:
            {
                
                NSLog(@"正在使用数据流量");
                
                statuStr = @"正在使用数据流量";
                
                break;
            }
                
            case RealStatusViaWiFi:
            {
                
                NSLog(@"正在使用WIFI");
                statuStr = @"正在使用WIFI";
                
                
                break;
            }
            default:
                break;
        }
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        NSLog(@"是否使用了弹窗");
        
        hud.mode = MBProgressHUDModeText;
        
        hud.labelText = statuStr;
        
        hud.yOffset +=100;
        
        [hud hide:YES afterDelay:1.f];
        
    }];
}

-(void)didReceiveBuddyRequest:(NSString *)username message:(NSString *)message{
    
    NSLog(@"接受到好友请求");
    
    if ([[AVUser currentUser][@"voice"] isEqualToString:@"off"]) {
        
        
    }else{
        
        [SKXFriendManager phoneVoice];
        
    }
    
    if ([[AVUser currentUser][@"shock"] isEqualToString:@"off"]) {
        
    }else{
        
        [SKXFriendManager phoneVibrate];
    }

    
    
    //请求网络数据
    AVQuery *userQuery = [AVQuery queryWithClassName:@"_User"];
    
    [userQuery includeKey:@"username"];
    
    [userQuery whereKey:@"username" equalTo:username];
    
    [userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        
        AVObject *obj = [objects lastObject];
        
        //设置系统消息
        SystemMessage *sys = [[SystemMessage alloc] init];
        sys.accepter = obj[@"nickname"];
        //设置系统消息类型
        sys.systemMessageType = SystemMessageTypeFriendAccept;
        [sys setMessageTextWithType:SystemMessageTypeFriendAccept];
        [sys setAcceptTimeStamp];
        
        sys.accepterObject = obj;
        [[SKXFriendManager sharaFriendManager].friendAcceptArray addObject:sys];
        //创建一个消息对象
        NSNotification * notice = [NSNotification notificationWithName:systemMessageChange object:nil userInfo:nil];
        //发送消息
        [[NSNotificationCenter defaultCenter]postNotification:notice];
        
    }];

    
}


@end
